<?php /* УТФ-8:fix */

class SPIcontroller__front__link extends SPIcontroller__dummy
{
    public function parse_env()
    { 
        $varstring = $_SERVER['REQUEST_URI'];
        $varstring = preg_replace( '|' . preg_quote('index.html') . '$|ui', '', $varstring );
        $varstring = trim( $varstring, '/' );
        $varstring = preg_replace( '|(/+)|ui', '/', $varstring );

        // ------------------------------------------------------------------------
        // Set default values

        $env_lang  = null;
        $env_topic  = null;
        $env_action = null;
        $env_vars   = array();

        $vars  = explode( '/', $varstring );
        $index = 0;

        foreach( $vars as $var )
        {
            if( $var == '' )
            {
                continue;
            }

            $index++;

            if( iconv_strpos( $var, '-' ) === false )
            {
                if( $index == 1 )
                {
                    $env_lang = $var;
                    continue;
                }
				
				if( $index == 2 )
                {
                    $env_topic = $var;
                    continue;
                }
                elseif( $index == 3 )
                {
                    $env_action = $var;
                    continue;
                }
                else { /* continue executing */ }
            }

            list( $key, $value ) = explode( '-', $var, 2 );

            if( is_numeric( $value ) === true )
            {
                $value = iconv_strpos( $value, '.' ) === false ? (int) $value : (float) $value;
            }

            $env_vars[$key] = $value;
        }
		
        return array( rtrim($env_lang,'.php'), $env_topic, $env_action, $env_vars );
    }

    private function pack_link( $topic_id = null, $action_id = null, $vars = null, $stdvars = null, $default_file = null, $lang = null )
    {
        $topic_id = (string) $topic_id;

        $res = SPIcfg::$dir['pubsite_root'];
		
		$lang = $lang ? $lang : SPI::I('lang')->get_lang();
		$lang = $lang.( SPIcfg::$isShowScriptExt === true ? '.php' : '' );

        if( $topic_id == '' )
        {
            return $res.$lang.'/';
        }

        $res .= $lang."/{$topic_id}/" . ( $action_id ? "{$action_id}/" : '' );

        // ------------------------------------------------------------------------

        if( is_array( $vars ) === true )
        foreach( $vars as $key => $value )
        {
            if( $value == '' )
            {
                continue;
            }

            $res .= urlencode( "{$key}-{$value}" ) . '/';
        }

        // ------------------------------------------------------------------------

        $stdvars_string = '';

        if( is_array( $stdvars ) === true )
        {
            foreach( $stdvars as $key => $value )
            {
                if( empty( $value ) )
                {
                    continue;
                }

                $stdvars_string .= "&{$key}=" . urlencode( $value );
            }

            $stdvars_string = ltrim( $stdvars_string, '&' );
        }

        // ------------------------------------------------------------------------

        $res .= ( $default_file ? $default_file : 'index.html' )
              . ( $stdvars_string ? '?'.$stdvars_string : '' );

        return $res;
    }

    public function prepare_translit_filename( $filename )
    {
        $filename = trim( $filename );

        if( $filename == '' )
        {
            return false;
        }

        $filename = strtolower( SPI::I('util')->string_to_translit( $filename, 'UTF-8', '_' ) );

        if( $filename == '' || $filename == '_' )
        {
            return false;
        }

        return $filename.'.html';
    }

    ###########################################################################

    public function absolute( $link )
    {
        if( $link )
        {
            return trim( SPIcfg::$hostnames['base'], '/' ) . $link;
        }

        return SPIcfg::$hostnames['base'];
    }

    public function homepage()
    {
        return $this->pack_link();
    }

    public function standard( $topic_id, $action_id = null, $vars = null )
    {
        return $this->pack_link( $topic_id, $action_id, $vars );
    }
	
    public function lang( $lang )
    {
        return $this->pack_link( null, null, null, null, null, $lang  );
    }

    public function selflink( $stdvars = null )
    {
        $selflink = rtrim( $_SERVER['REQUEST_URI'], '?' );

        // ------------------------------------------------------------------------

        $stdvars_string = '';

        if( is_array( $stdvars ) === true )
        {
            foreach( $stdvars as $key => $value )
            {
                if( empty( $value ) )
                {
                    continue;
                }

                $stdvars_string .= "&{$key}=" . urlencode( $value );
            }

            if( strpos( $selflink, '?' ) === false )
            {
                $stdvars_string = '?'.substr( $stdvars_string, 1 );
            }
        }

        // ------------------------------------------------------------------------

        return $selflink.$stdvars_string;
    }

    public function minify( $type )
    {
        return SPIcfg::$dir['pubsite_root'] . 'minify.php?type=front-'.$type.'&build='.SPIcfg::$util['minify']['build'];
    }

    ###########################################################################

    public function content( $type, $content_id = null, $catId = null, $page_index = null, $translit_name = null, $filters = null )
    {
        $vars = array();
        if( $content_id ) { $vars['i'] = $content_id; }
        if( $catId ) { $vars['cat'] = $catId; }

        $page_index = max( 1, (int) $page_index );
        if( $page_index > 1 ) { $vars['page'] = $page_index; }
		
		if( is_array( $filters ) )
			$vars['filter'] = join( '-', $filters );

        return $this->pack_link( $type, null, $vars, null, $this->prepare_translit_filename( $translit_name ) );
    }

    public function profile( $type, $profile_id = null, $translit_name = null )
    {
        $vars = array();
        if( $profile_id ) { $vars['i'] = $profile_id; }

        return $this->pack_link( $type, null, $vars, null, $this->prepare_translit_filename( $translit_name ) );
    }

    public function page( $page_id )
    {
        return $this->pack_link( $page_id );
    }
	
    public function catalog( $catId, $page = 1, $name = null)
    {
        $vars = array();
        if( $catId ) { $vars['i'] = $catId; }

        $page = max( 1, (int) $page );
        if( $page > 1 ) { $vars['page'] = $page; }
		
        return $this->pack_link( 'catalog', null, $vars, null, $this->prepare_translit_filename( $name ) );
    }
}
